package edu.cqie.smartfarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.cqie.smartfarm.constant.MessageConstant;
import edu.cqie.smartfarm.dto.DictionaryDTO;
import edu.cqie.smartfarm.dto.DictionaryQueryDTO;
import edu.cqie.smartfarm.entity.DictionaryData;
import edu.cqie.smartfarm.mapper.DictionaryDataMapper;
import edu.cqie.smartfarm.result.PageVO;
import edu.cqie.smartfarm.service.IDictionaryDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.cqie.smartfarm.vo.DictionaryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyw
 * @since 2024-12-26
 */
@Service
@RequiredArgsConstructor
public class DictionaryDataServiceImpl extends ServiceImpl<DictionaryDataMapper, DictionaryData> implements IDictionaryDataService {
    private final DictionaryDataMapper dictionaryDataMapper;

    /**
     * 获取字典数据列表
     * @param dictionaryQueryDTO
     * @return
     */
    @Override
    public PageVO<DictionaryVO> getValueList(DictionaryQueryDTO dictionaryQueryDTO) {
        Page<DictionaryData> page = new Page<>(dictionaryQueryDTO.getPageNumber(), dictionaryQueryDTO.getPageSize());

        LambdaQueryWrapper<DictionaryData> lambdaQueryWrapper = new LambdaQueryWrapper<DictionaryData>()
                .eq(DictionaryData::getDictionaryListId, dictionaryQueryDTO.getDictionaryListId())
                .eq(dictionaryQueryDTO.getStatus() != null,DictionaryData::getStatus, dictionaryQueryDTO.getStatus())
                .like(dictionaryQueryDTO.getValue() != null && dictionaryQueryDTO.getValue().length() > 0, DictionaryData::getValue, dictionaryQueryDTO.getValue());
        Page<DictionaryData> dictionaryDataPage = dictionaryDataMapper.selectPage(page, lambdaQueryWrapper);
        List<DictionaryVO> dictionaryVOList = BeanUtil.copyToList(dictionaryDataPage.getRecords(), DictionaryVO.class);

        PageVO<DictionaryVO> pageVO = new PageVO<>();
        pageVO.setCurrent(dictionaryDataPage.getCurrent());
        pageVO.setSize(dictionaryDataPage.getSize());
        pageVO.setTotal(dictionaryDataPage.getTotal());
        pageVO.setPages(dictionaryDataPage.getPages());
        pageVO.setRecords(dictionaryVOList);
        return pageVO;
    }

    @Override
    public void addValue(DictionaryDTO dictionaryDTO) {
        LambdaQueryWrapper<DictionaryData> queryWrapper = new LambdaQueryWrapper<DictionaryData>()
                .eq(DictionaryData::getValue, dictionaryDTO.getValue())
                .eq(DictionaryData::getDictionaryListId, dictionaryDTO.getDictionaryListId());
        if (count(queryWrapper) > 0) {
            throw new RuntimeException(MessageConstant.DICTIONARY_EXISTS);
        }
        DictionaryData dictionaryData = BeanUtil.copyProperties(dictionaryDTO, DictionaryData.class);
        save(dictionaryData);
    }
}
